# Maintainer: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>

_realname=sip
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.18
pkgrel=1
pkgdesc="Tool to create Python bindings for C and C++ libraries (mingw-w64)"
arch=('any')
license=('GPL')
url="http://www.riverbankcomputing.co.uk/software/sip"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3")
source=("https://downloads.sourceforge.net/project/pyqt/${_realname}/${_realname}-${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-mingw-python.patch")
sha256sums=('f1dc5c81c07a9ad97edcd4a0af964a41e420024ba7ca165afd2b351efd249cb6'
            'eab49324d351582f3f7d5da772a009fdf54d2a8366ed28bba2318a7b384a980c')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  # Apply patches
  patch -p1 -i "${srcdir}"/0001-mingw-python.patch
}

build() {
  for builddir in python{2,3}-${MINGW_CHOST}; do
    [[ -d $builddir ]] && rm -rf ${builddir}
    cp -r ${_realname}-${pkgver} ${builddir}
    pushd ${builddir}
    ${MINGW_PREFIX}/bin/${builddir%-${MINGW_CHOST}} configure.py \
      CFLAGS="${CFLAGS}" LFLAGS="${LDFLAGS}"
    make
    popd
  done
}

package_sip() {
  pkgdesc="A tool that makes it easy to create Python bindings for C and C++ libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")

  cd python2-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install -C sipgen

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_python3-sip() {
  pkgdesc="Python 3.x SIP bindings for C and C++ libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-sip" "${MINGW_PACKAGE_PREFIX}-python3")

  cd python3-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install -C siplib

  install -Dm644 sipconfig.py "${pkgdir}${MINGW_PREFIX}"/lib/python3.5/site-packages/sipconfig.py
  install -Dm644 sipdistutils.py "${pkgdir}${MINGW_PREFIX}"/lib/python3.5/site-packages/sipdistutils.py

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-sip/LICENSE"
}

package_python2-sip() {
  pkgdesc="Python 2.x SIP bindings for C and C++ libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-sip" "${MINGW_PACKAGE_PREFIX}-python2")

  cd python2-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install -C siplib

  install -Dm644 sipconfig.py "${pkgdir}${MINGW_PREFIX}"/lib/python2.7/site-packages/sipconfig.py
  install -Dm644 sipdistutils.py "${pkgdir}${MINGW_PREFIX}"/lib/python2.7/site-packages/sipdistutils.py

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-sip/LICENSE"
}

package_mingw-w64-i686-sip() {
  package_sip
}

package_mingw-w64-i686-python2-sip() {
  package_python2-sip
}

package_mingw-w64-i686-python3-sip() {
  package_python3-sip
}

package_mingw-w64-x86_64-sip() {
  package_sip
}

package_mingw-w64-x86_64-python2-sip() {
  package_python2-sip
}

package_mingw-w64-x86_64-python3-sip() {
  package_python3-sip
}
